package com.bupt.liutong.admin.core.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.core.bo.RoleBo;
import com.bupt.liutong.admin.core.form.RoleForm;
import com.bupt.liutong.util.BeanUtils;

public class RoleAction extends BaseAction {
	
	private RoleBo roleBo;
	
	public ActionForward getRolesByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleForm roleForm = (RoleForm) form;
		request.setAttribute("roleFormList", roleBo.getRolesByPage(roleForm, request));
		putPageInfo(request, roleForm);
		return mapping.findForward("getRolesByPageSuccess");
	}
	
	public ActionForward insertRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BeanUtils.copyProperties(form, new RoleForm());
		return mapping.findForward("insertRoleSuccess");
	}
	
	public ActionForward insertRoleDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		roleBo.insertRole((RoleForm)form);
		BeanUtils.copyProperties(form, new RoleForm());
		return mapping.findForward("insertRoleDoSuccess");
	}
	
	public ActionForward updateRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleForm roleForm = (RoleForm) form;
		BeanUtils.copyProperties(form, roleBo.getRoleById(roleForm));
		return mapping.findForward("updateRoleSuccess");
	}
	
	public ActionForward updateRoleDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		roleBo.updateRole((RoleForm)form);
		return mapping.findForward("updateRoleDoSuccess");
	}
	
	public ActionForward updateRoleModuleAndOper(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleForm roleForm = (RoleForm) form;
		request.setAttribute("roleModuleAndOperMap", roleBo.getRoleModulesAndOpersByRoleId(roleForm));
		return mapping.findForward("updateRoleModuleAndOperSuccess");
	}
	
	public ActionForward updateRoleModuleAndOperDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleForm roleForm = (RoleForm) form;
		String[] moduleIds = request.getParameterValues("moduleId");
		String[] operIds = request.getParameterValues("operId");
		roleBo.updateRoleModule(roleForm, moduleIds);
		roleBo.updateRoleOper(roleForm, operIds);
		return mapping.findForward("updateRoleModuleAndOperDoSuccess");
	}
	
	public ActionForward deleteRoles(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		roleBo.deleteRoles(ids);
		return mapping.findForward("deleteRolesSuccess");
	}

	public void setRoleBo(RoleBo roleBo) {
		this.roleBo = roleBo;
	}
}

